package com.hengyue.quartz.service.impl;


import com.hengyue.common.utils.DateUtils;
import com.hengyue.common.utils.StringUtils;
import com.hengyue.quartz.domain.SysJobLog;
import com.hengyue.quartz.repository.SysJobLogRepository;
import com.hengyue.quartz.service.ISysJobLogService;
import jakarta.persistence.criteria.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SysJobLogServiceImpl implements ISysJobLogService {

    private final SysJobLogRepository sysJobLogRepository;

    @Override
    public Page<SysJobLog> selectJobLogList(SysJobLog jobLog, Pageable pageable) {
        return sysJobLogRepository.findAll((root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (StringUtils.isNotEmpty(jobLog.getJobName())) {
                predicates.add(cb.like(root.get("jobName"), "%" + jobLog.getJobName() + "%"));
            }
            if (StringUtils.isNotEmpty(jobLog.getJobGroup())) {
                predicates.add(cb.equal(root.get("jobGroup"), jobLog.getJobGroup()));
            }
            if (StringUtils.isNotEmpty(jobLog.getStatus())) {
                predicates.add(cb.equal(root.get("status"), jobLog.getStatus()));
            }
            if (StringUtils.isNotEmpty(jobLog.getInvokeTarget())) {
                predicates.add(cb.like(root.get("invokeTarget"), "%" + jobLog.getInvokeTarget() + "%"));
            }
            if (jobLog.getParams() != null) {
                if (StringUtils.isNotEmpty(jobLog.getParams().get("beginTime"))) {
                    Date beginTime = DateUtils.parseDate(jobLog.getParams().get("beginTime"));
                    predicates.add(cb.greaterThanOrEqualTo(root.get("createTime"), beginTime));
                }
                if (StringUtils.isNotEmpty(jobLog.getParams().get("endTime"))) {
                    Date endTime = DateUtils.parseDate(jobLog.getParams().get("endTime"));
                    predicates.add(cb.lessThanOrEqualTo(root.get("createTime"), endTime));
                }
            }
            query.orderBy(cb.desc(root.get("createTime")));
            return cb.and(predicates.toArray(new Predicate[0]));
        },pageable);
    }



    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return sysJobLogRepository.findById(jobLogId)
                .orElseThrow(() -> new RuntimeException("任务日志不存在"));
    }

    @Override
    @Transactional
    public void addJobLog(SysJobLog jobLog) {
        sysJobLogRepository.save(jobLog);
    }

    @Override
    @Transactional
    public void deleteJobLogByIds(Long[] logIds) {
        sysJobLogRepository.deleteAllByIdInBatch(Arrays.asList(logIds));
    }

    @Override
    @Transactional
    public void deleteJobLogById(Long jobId) {
        sysJobLogRepository.deleteById(jobId);
    }

    @Override
    @Transactional
    public void cleanJobLog() {
        sysJobLogRepository.deleteAllInBatch();
    }
}

